package com.wenyu.wenyuyuedu.utils;

import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/4/6.
 */

public class TimeStyle {
    /*发布的时间*/
    public static String publishTime(String create_time) {
        /*获取系统时间*/
        long systemTime = System.currentTimeMillis();
        String s = String.valueOf(systemTime);
        Log.e("11111", systemTime + "---" + s);
        Calendar systemC = Calendar.getInstance();
        /*年*/
        int systemYear = systemC.get(Calendar.YEAR);
        /*月*/
        int systeMonth = systemC.get(Calendar.MONTH) + 1;
        /*日*/
        int systemDay = systemC.get(Calendar.DAY_OF_MONTH);
        /*时*/
        int systemHour = systemC.get(Calendar.HOUR_OF_DAY);
        /*分*/
        int systemMinute = systemC.get(Calendar.MINUTE);
        Log.e("11111", systemYear + "---" + systeMonth + "---" + systemDay + "---" + systemHour + "-----" + systemMinute);
        /*以下是服务器返回的时间*/
        long serverTime = Long.valueOf(create_time) * 1000;
        Date date = new Date(serverTime);
        /*获取服务器返回过来的年份*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String yearS = sdf.format(date);
        int serverYear = Integer.parseInt(yearS);
        /*获取服务器返回过来的月份*/
        int serverMonth = date.getMonth() + 1;
        /*获取服务器返回过来的日份*/
        int serverDay = date.getDate();
        /*获取服务器返回过来的小时*/
        int serverHours = date.getHours();
        /*获取服务器返回过来的分钟*/
        int serverMinutes = date.getMinutes();
        Log.e("11111服务器时间", serverYear + "---" + serverMonth + "---" + serverDay + "---" + serverHours + "-----" + serverMinutes);
        StringBuilder timeString = new StringBuilder();
        //返回字符串
        String realTime;
        // 24小时以内
        if (systemTime - serverTime <= 1000 * 60 * 60 * 24) {
            if (systemTime - serverTime < 1000 * 60 * 60) {
                if (systemTime - serverTime < 1000 * 60) {
                    realTime = (systemTime - serverTime) / 1000 + "秒";
                    return "刚刚";
                }
                realTime = ((systemTime - serverTime) / (1000 * 60)) + "分钟前";
                return realTime;
            }
            realTime = ((systemTime - serverTime) / (1000 * 60 * 60)) + "小时前";
            return realTime;
        }
        // 当月三天3天之内的判断
        if (systemDay > 2 && systeMonth == serverMonth) {
            if (systemDay - serverDay == 1) {
                // 昨天
                realTime = "昨天  " + serverHours + ":" + serverMinutes + "";
                return realTime;
            } else if (systemDay - serverDay == 2) {
                //前天
                realTime = "前天  " + serverHours + ":" + serverMinutes + "";
                return realTime;
            }
        }
        // 夸月三天判断
        if (systemDay < 3) {
            if (systeMonth - serverMonth == 0) {
                if (systemDay - serverDay == 1) {
                    // 昨天
                    realTime = "昨天  " + serverHours + ":" + serverMinutes + "";
                    return realTime;
                } else if (systemDay - serverDay == 2) {
                    //前天
                    realTime = "前天  " + serverHours + ":" + serverMinutes + "";
                    return realTime;
                }
            } else if (systeMonth - serverMonth == 1) {
                if (systemDay + 30 - serverDay == 1) {
                    // 昨天
                    realTime = "昨天  " + serverHours + ":" + serverMinutes + "";
                    return realTime;
                } else if (systemDay + 30 - serverDay == 2) {
                    //前天
                    realTime = "前天  " + serverHours + ":" + serverMinutes + "";
                    return realTime;
                }
            }
        }
        if (systemYear > serverYear){
            realTime = serverYear + "年" +  serverMonth + "月" + serverDay + "日";
            return realTime;
        }
        realTime = serverMonth + "月" + serverDay + "日";
        return realTime;
    }
}
